﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace NetworkSupervisor.Helper
{
    public static class SearchHelper
    {
        public static string GetBaiduRemoteHtml(string keyword, int page, string site = "")
        {
            string html;
            string url;
            if (site != "") keyword = string.Format("site:{0} {1}", site, keyword);
            url = String.Format("http://www.baidu.com/s?wd={0}&pn={1}&tn=baiduadv&ie=utf-8", keyword, page);
            html = HtmlHelper.GetRemoteHtml(url, Encoding.UTF8);
            return html;
        }

        public static string GetSousouRemoteHtml(string keyword, int page, string site = "")
        {
            string html;
            string url;
            if (site != "") keyword = string.Format("site:{0} {1}", site, keyword);
            url = String.Format("http://www.soso.com/q?w={0}&lr=&sc=web&ch=w.p&num=10&gid=&cin=&site=&sf=0&sd=0&nf=0&pg={1}", keyword, page + 1);
            html = HtmlHelper.GetRemoteHtml(url, Encoding.Default);
            return html;
        }

        public static string GetYahooRemoteHtml(string keyword, int page, string site = "")
        {
            string html;
            string url;
            if (site != "") keyword = string.Format("site:{0} {1}", site, keyword);
            url = String.Format("http://www.yahoo.cn/s?q={0}&page={1}", keyword, page + 1);
            html = HtmlHelper.GetRemoteHtml(url, Encoding.UTF8);
            return html;
        }

        /// <summary>
        /// 百度最新，包括新闻、博客、论坛、视频
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static string GetBaiduLatestRemoteHtml(string keyword, int page)
        {
            string html;
            string url;
            url = String.Format("http://www.baidu.com/s?wd={0}&pn={1}0&tn=baidurt&rtt=4&bsst=1", keyword, page);
            html = HtmlHelper.GetRemoteHtml(url, Encoding.Default);
            return html;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static string GetSinaWeiboRemoteHtml(string keyword)
        {
            string html1 = "", html2 = "";
            string url;
            url = String.Format("http://s.weibo.com/weibo/{0}&xsort=hot&scope=ori", keyword);
            html1 = HtmlHelper.GetRemoteHtml(url, Encoding.UTF8);
            html1 = EncodingHelper.Unicode2Character(html1);
            html1 = html1.Replace("\\", "");
            url = String.Format("http://s.weibo.com/weibo/{0}&scope=ori", keyword);
            html2 = HtmlHelper.GetRemoteHtml(url, Encoding.UTF8);
            html2 = EncodingHelper.Unicode2Character(html2);
            html2 = html2.Replace("\\", "");

            return html1 + html2;
        }


        /// <summary>
        /// 返回Groups:href,title,content,datetime
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MatchCollection GetYahooMatchCollection(string html)
        {
            string reg;
            MatchCollection matchCollection;
            reg = @"<h3\sclass=""title""><a.*?href=""(?<href>[^""]+?)"">(?<title>.*?)</a></h3>.*?all;"">(?<content>.*?)</div>.*?""date"">(?<datetime>[^<]*?)</span>";
            matchCollection = Regex.Matches(html, reg);
            return matchCollection;
        }

        /// <summary>
        /// 返回Groups:href,title,content,datetime
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MatchCollection GetSousouMatchCollection(string html)
        {
            string reg;
            MatchCollection matchCollection;
            reg = @"<h3><a[ ]href=""(?<href>[^""]+?)"".*?>(?<title>.+?)</a>.+?ds"">(?<content>.+?)</p>.+?&nbsp;(?<datetime>.{1,10}?)</cite>";
            matchCollection = Regex.Matches(html, reg);
            return matchCollection;
        }

        /// <summary>
        /// 返回Groups:href,title,content,datetime
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MatchCollection GetBaiduMatchCollection(string html)
        {
            string reg;
            MatchCollection matchCollection;
            reg = @"<a\s{2}data-click=.+?href=""(?<href>[^""]+?)"".*?>(?<title>.+?)</a></h3>[ ]<font[ ]size=-1>(?<content>.+?)<br>.+?\s(?<datetime>.[^\s]*?)\s{2,3}</span>";
            matchCollection = Regex.Matches(html, reg);
            return matchCollection;
        }

        /// <summary>
        /// 返回Groups:href,title,content,datetime,from
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MatchCollection GetBaiduLatestMatchCollection(string html)
        {
            string reg;
            MatchCollection matchCollection;
            reg = @"<a\sonmousedown.*?href=""(?<href>[^""]*?)""[^>].*?>(?<title>.*?)</a>.*?realtime"">(?<from>[^-]*?)[\s-]*?(?<datetime>\d[^-<]*?)</div>(?<content>.*?)<br>";
            matchCollection = Regex.Matches(html, reg);
            return matchCollection;
        }

        /// <summary>
        /// 返回Groups:href,title,content,datetime,from
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MatchCollection GetSinaWeiboMatchCollection(string html)
        {
            string reg;
            MatchCollection matchCollection;
            reg = @"<a.nick.*?href=""(?<href>[^""]*?)""[^>]*?name>(?<title>[^<]*?)<[^：]*?a>：<em>(?<content>.*?)</em>.*?name>(?<datetime>[^<>]*?)</a>.*?nofollow"">(?<from>[^<]*?)</a>";
            matchCollection = Regex.Matches(html, reg);
            return matchCollection;
        }

        public static IList<ArticleInfo> GetBaiduArticleList(MatchCollection matchCollection, string type, string keyword)
        {
            IList<ArticleInfo> articleList = new List<ArticleInfo>();
            foreach (Match match in matchCollection)
            {
                if (match.Success)
                {
                    string title = HtmlHelper.RemoveHtmlTag(match.Groups["title"].Value);
                    string href = HtmlHelper.RemoveHtmlTag(match.Groups["href"].Value);
                    string content = HtmlHelper.RemoveHtmlTag(match.Groups["content"].Value);
                    DateTime datetime;
                    string strTime = match.Groups["datetime"].Value;
                    if (strTime.EndsWith("小时前") || strTime.EndsWith("分钟前"))
                    {
                        datetime = DateTime.Today;
                    }
                    else if (strTime.EndsWith("天前"))
                    {
                        int dayDiff = Convert.ToInt32(strTime.Substring(0, 1));
                        datetime = DateTime.Today.AddDays(-dayDiff);
                    }
                    else
                    {
                        datetime = Convert.ToDateTime(strTime);
                    }

                    ArticleInfo articleInfo = new ArticleInfo();
                    articleInfo.Title = title;
                    articleInfo.Href = href;
                    articleInfo.Content = content;
                    articleInfo.Datetime = datetime;
                    articleInfo.Type = type;
                    articleInfo.Keyword = keyword;
                    articleInfo.From = "百度";
                    articleList.Add(articleInfo);
                }
            }
            return articleList;
        }

        public static IList<ArticleInfo> GetSousouArticleList(MatchCollection matchCollection, string type, string keyword)
        {
            IList<ArticleInfo> articleList = new List<ArticleInfo>();
            foreach (Match match in matchCollection)
            {
                if (match.Success)
                {
                    string title = HtmlHelper.RemoveHtmlTag(match.Groups["title"].Value);
                    string href = HtmlHelper.RemoveHtmlTag(match.Groups["href"].Value);
                    string content = HtmlHelper.RemoveHtmlTag(match.Groups["content"].Value);
                    DateTime datetime;
                    string strTime = match.Groups["datetime"].Value;
                    if (strTime.EndsWith("小时前") || strTime.EndsWith("分钟前"))
                    {
                        datetime = DateTime.Today;
                    }
                    else if (strTime.EndsWith("天前"))
                    {
                        int dayDiff = Convert.ToInt32(strTime.Substring(0, 1));
                        datetime = DateTime.Today.AddDays(-dayDiff);
                    }
                    else
                    {
                        datetime = Convert.ToDateTime(strTime);
                    }

                    ArticleInfo articleInfo = new ArticleInfo();
                    articleInfo.Title = title;
                    articleInfo.Href = href;
                    articleInfo.Content = content;
                    articleInfo.Datetime = datetime;
                    articleInfo.Type = type;
                    articleInfo.Keyword = keyword;
                    articleInfo.From = "搜搜";
                    articleList.Add(articleInfo);
                }
            }
            return articleList;
        }

        public static IList<ArticleInfo> GetYahooArticleList(MatchCollection matchCollection, string type, string keyword)
        {
            IList<ArticleInfo> articleList = new List<ArticleInfo>();
            foreach (Match match in matchCollection)
            {
                if (match.Success)
                {
                    string title = HtmlHelper.RemoveHtmlTag(match.Groups["title"].Value);
                    string href = HtmlHelper.RemoveHtmlTag(match.Groups["href"].Value);
                    string content = HtmlHelper.RemoveHtmlTag(match.Groups["content"].Value);
                    DateTime datetime;
                    string strTime = match.Groups["datetime"].Value;
                    if (strTime.EndsWith("小时前") || strTime.EndsWith("分钟前"))
                    {
                        datetime = DateTime.Today;
                    }
                    else if (strTime.EndsWith("天前"))
                    {
                        int dayDiff = Convert.ToInt32(strTime.Substring(0, 1));
                        datetime = DateTime.Today.AddDays(-dayDiff);
                    }
                    else
                    {
                        datetime = Convert.ToDateTime(strTime);
                    }

                    ArticleInfo articleInfo = new ArticleInfo();
                    articleInfo.Title = title;
                    articleInfo.Href = href;
                    articleInfo.Content = content;
                    articleInfo.Datetime = datetime;
                    articleInfo.Type = type;
                    articleInfo.Keyword = keyword;
                    articleInfo.From = "雅虎";
                    articleList.Add(articleInfo);
                }
            }
            return articleList;
        }

        public static IList<ArticleInfo> GetSinaWeiboArticleList(MatchCollection matchCollection, string type, string keyword)
        {
            IList<ArticleInfo> articleList = new List<ArticleInfo>();
            foreach (Match match in matchCollection)
            {
                if (match.Success)
                {
                    string title = HtmlHelper.RemoveHtmlTag(match.Groups["title"].Value);
                    string href = HtmlHelper.RemoveHtmlTag(match.Groups["href"].Value);
                    string content = HtmlHelper.RemoveHtmlTag(match.Groups["content"].Value);
                    string from = HtmlHelper.RemoveHtmlTag(match.Groups["from"].Value);

//6秒
//10秒
//20秒前
//5分钟
//53分钟前
//今天 19:25
//今天 19:10
//9月14日 12:57
                    DateTime datetime = DateTime.Now;
                    string strTime = match.Groups["datetime"].Value;
                    if (strTime.Contains("秒"))
                    {
                        string obj = strTime.Substring(0, strTime.IndexOf("秒"));
                        int seconds = Convert.ToInt32(obj);
                        datetime = DateTime.Now.AddSeconds(-seconds);
                    }
                    else if (strTime.Contains("分"))
                    {
                        string obj = strTime.Substring(0, strTime.IndexOf("分"));
                        int minites = Convert.ToInt32(obj);
                        datetime = DateTime.Now.AddMinutes(-minites);
                    }
                    else if (strTime.Contains("今天"))
                    {
                        int hour = Convert.ToInt32(strTime.Substring(3, 2));
                        int minite = Convert.ToInt32(strTime.Substring(6, 2));
                        datetime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minite, 0);
                    }
                    else if (strTime.Contains("月"))
                    {
                        MatchCollection matches = Regex.Matches(strTime, @"(?<month>\d{1,2})[^\d]*?(?<day>\d{1,2})[^\d]*?(?<hour>\d{1,2})[^\d]*?(?<minite>\d{1,2})", RegexOptions.IgnoreCase);
                        int month = Convert.ToInt32(matches[0].Groups["month"].Value);
                        int day = Convert.ToInt32(matches[0].Groups["day"].Value);
                        int hour = Convert.ToInt32(matches[0].Groups["hour"].Value);
                        int minite = Convert.ToInt32(matches[0].Groups["minite"].Value);
                        datetime = new DateTime(DateTime.Now.Year, month, day, hour, minite, 0);
                    }
                    else
                    {
                        datetime = DateTime.Now;
                    }

                    ArticleInfo articleInfo = new ArticleInfo();
                    articleInfo.Title = title;
                    articleInfo.Href = href;
                    articleInfo.Content = content;
                    articleInfo.Datetime = datetime;
                    articleInfo.Type = type;
                    articleInfo.Keyword = keyword;
                    articleInfo.From = from;
                    articleList.Add(articleInfo);
                }
            }
            return articleList;
        }

        /// <summary>
        /// 获取百度最新
        /// </summary>
        /// <param name="matchCollection"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IList<ArticleInfo> GetBaiduLatestArticleList(MatchCollection matchCollection, string type, string keyword)
        {
            IList<ArticleInfo> articleList = new List<ArticleInfo>();
            foreach (Match match in matchCollection)
            {
                if (match.Success)
                {
                    string title = HtmlHelper.RemoveHtmlTag(match.Groups["title"].Value);
                    string href = HtmlHelper.RemoveHtmlTag(match.Groups["href"].Value);
                    string content = HtmlHelper.RemoveHtmlTag(match.Groups["content"].Value);
                    string from = HtmlHelper.RemoveHtmlTag(match.Groups["from"].Value);
                    DateTime datetime;
                    string strTime = match.Groups["datetime"].Value;
                    if (strTime.EndsWith("分钟前"))
                    {
                        int mimutes = Convert.ToInt32(strTime.Replace("分钟前", ""));
                        datetime = DateTime.Now.AddMinutes(-mimutes);
                    }
                    else if (strTime.EndsWith("小时前"))
                    {
                        int hours = Convert.ToInt32(strTime.Replace("小时前", ""));
                        datetime = DateTime.Now.AddHours(-hours);
                    }
                    else if (strTime.EndsWith("天前"))
                    {
                        int days = Convert.ToInt32(strTime.Replace("天前", ""));
                        datetime = DateTime.Now.AddDays(-days);
                    }
                    else
                    {
                        datetime = Convert.ToDateTime(strTime);
                    }


                    ArticleInfo articleInfo = new ArticleInfo();
                    articleInfo.Title = title;
                    articleInfo.Href = href;
                    articleInfo.Content = content;
                    articleInfo.Datetime = datetime;
                    articleInfo.Type = type;
                    articleInfo.From = from;
                    articleInfo.Keyword = keyword;
                    articleList.Add(articleInfo);
                }
            }
            return articleList;
        }

        public static IList<ArticleInfo> GetBaiduLatestArticleList(string keyword, int page, string type)
        {
            string html = GetBaiduLatestRemoteHtml(keyword, page);
            MatchCollection matchCollection = GetBaiduLatestMatchCollection(html);
            IList<ArticleInfo> articleInfos = GetBaiduLatestArticleList(matchCollection, type, keyword);
            return articleInfos;
        }

        public static IList<ArticleInfo> GetSinaWeiboArticleList(string keyword, string type)
        {
            string html = GetSinaWeiboRemoteHtml(keyword);
            MatchCollection matchCollection = GetSinaWeiboMatchCollection(html);
            IList<ArticleInfo> articleInfos = GetSinaWeiboArticleList(matchCollection, type, keyword);
            return articleInfos;
        }

        public static IList<ArticleInfo> GetBaiduArticleList(string keyword, int page, string type, string site = "")
        {
            string html = GetBaiduRemoteHtml(keyword, page, site);
            MatchCollection collection = GetBaiduMatchCollection(html);
            IList<ArticleInfo> articleInfos = GetBaiduArticleList(collection, type, keyword);
            return articleInfos;
        }

        public static IList<ArticleInfo> GetSousouArticleList(string keyword, int page, string type, string site = "")
        {
            string html = GetSousouRemoteHtml(keyword, page, site);
            MatchCollection collection = GetSousouMatchCollection(html);
            IList<ArticleInfo> articleInfos = GetSousouArticleList(collection, type, keyword);
            return articleInfos;
        }

        public static IList<ArticleInfo> GetYahooArticleList(string keyword, int page, string type, string site = "")
        {
            string html = GetYahooRemoteHtml(keyword, page, site);
            MatchCollection collection = GetYahooMatchCollection(html);
            IList<ArticleInfo> articleInfos = GetYahooArticleList(collection, type, keyword);
            return articleInfos;
        }

        public static IList<ArticleInfo> GetAllArticleList(string keyword, int page, string type, string site = "")
        {
            IList<ArticleInfo> articleList = new List<ArticleInfo>();

            IList<ArticleInfo> baiduArticleList = GetBaiduArticleList(keyword, page, type, site);
            IList<ArticleInfo> sousouArticleList = GetSousouArticleList(keyword, page, type, site);
            IList<ArticleInfo> yahooArticleList = GetYahooArticleList(keyword, page, type, site);

            foreach (ArticleInfo info in baiduArticleList)
            {
                if (articleList.All(o => o.Title != info.Title)) articleList.Add(info);
            }
            foreach (ArticleInfo info in sousouArticleList)
            {
                if (articleList.All(o => o.Title != info.Title)) articleList.Add(info);
            }
            foreach (ArticleInfo info in yahooArticleList)
            {
                if (articleList.All(o => o.Title != info.Title)) articleList.Add(info);
            }

            return articleList;
        }

        public static IList<ArticleInfo> GetAllArticleList(string keyword, int page, string type, string[] sites)
        {
            if (sites.Length < 1) return null;

            IList<ArticleInfo> articleList = new List<ArticleInfo>();
            foreach (string site in sites)
            {
                IList<ArticleInfo> infos = GetAllArticleList(keyword, page, type, site);
                foreach (ArticleInfo info in infos)
                {
                    if (articleList.All(o => o.Title != info.Title)) articleList.Add(info);
                }
            }

            return articleList;
        }

        public static IList<ArticleInfo> GetLatestArticleList(string keyword, int page, string type)
        {
            IList<ArticleInfo> articleList = GetBaiduLatestArticleList(keyword, page, type);
            return articleList;
        }

        public static IList<ArticleInfo> GetWeiBoArticleList(string keyword, string type)
        {
            IList<ArticleInfo> articleList = GetSinaWeiboArticleList(keyword, type);
            return articleList;
        }
    }
}